define(["../com"], function() {
	
	var carNo = null;
	var $list = $(".points-page");
	var $carNos = $(".modal .list-group");
	$list.parseHtml().empty();	
	$carNos.parseHtml().empty();
	
	$(document).on('click', ".modal .list-group a", function() {
		$carNos.find("a").removeClass("active");
		$(this).addClass("active");
		$(".modal").modal("hide");
		setCarNo($(this).text());
		reloadList();
	})
	
	$.get(g_domain + "/car?r=" + Math.random(), function(data) {
		if (data.status !== 0) {
			data.code == "0" ? void(0) : alert(data.message);
			return;
		}
		$carNos.empty().parseHtml({ 
			data: data.result.list,
			beforeNode: {
				root: function(car) {
					car.activeClass = "";
					if (car.isDefault) {
						setCarNo(car.carNo);
						car.activeClass = "active";
					}
				} 
			}
		});
		reloadList();
	});
	
	function setCarNo(value) {
		$("#curCarNo-text").html(value);
		carNo = value;
	}
	
	function reloadList() {
		$list.parseHtml().empty();
		loadList(1);
	}
	
	function loadList(nowPage) {
		$.post(g_domain + "/user/qryrepair?page=" + nowPage, {carNo: carNo}, function(data) {
			if (data.status !== 0) {
				data.code == "0" ? void(0) : alert(data.message);
				return;
			}

			var apiData = $.parseJSON(data.result).response;
			if (apiData.length == 0) {
				alert("还没有维修记录");
				return;
			}
			
			$list.parseHtml({
				data: apiData,
				beforeNode: {
					root: function(item) {
						if (!item.FittingList) {
							item.FittingList = [];
						} else {
							item.FittingList = item.FittingList.replace(/\r\n/g,";").replace(/\|/g,";");
							var fitList = item.FittingList.split(";");
							item.FittingList = [];
							for (var i in fitList) {
								var fitting = (parseInt(i) + 1) + "." + fitList[i];
								item.FittingList.push({fitting: fitting});
							}
						}
						
						if (!item.CurMileage) {
							item.CurMileage = "--";
						}
					}
				}
			});
			
			if (apiData.length == 10) {
				loadList(nowPage + 1);
			}
		});
	}
	
});